# -*- coding: UTF-8 -*-
# https://blog.csdn.net/weixin_43605788/article/details/104655859
import datetime
import os
import time

from fastapi import FastAPI
from starlette.requests import Request
from starlette.responses import FileResponse
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates

from SaveText import Text
from main import RtasrClient

app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount('/static', StaticFiles(directory='static'), name='static')


@app.get("/")
async def main(request: Request):
    return templates.TemplateResponse('signin.html', {'request': request})


@app.get("/start")
async def start_record(request: Request):
    print("start button press...")
    # 开始录音，并向远方发送识别信息
    global client
    global text
    client = RtasrClient()
    text = Text()
    return "start now...."


@app.get("/endrecord")
async def end_record(request: Request):
    print("end button press...")
    # 开始录音，并向远方发送识别信息
    ### todo by wml
    global client
    if client is not None:
        client.close()

    return "end now...."


@app.get("/readinfo")
async def read_info(request: Request):
    curTime = time.time()
    myInfo = ""
    global client
    if client is not None:
        myInfo = client.readInfo()

    return myInfo


@app.get("/allinfo")
async def all_info(request: Request):
    curTime = time.time()
    myInfo = ""
    global client
    lists = client.allInfo()
    if client is not None:
        for i in lists:
            myInfo = "".join(lists)

    return "all info  now.{}".format(myInfo)

@app.get("/addinfo")  # 将转写的内容写入到word文档
async def add_info(request: Request):
    curTime = time.time()
    global text
    global client
    text = Text()
    text.addText().add_heading('Meeting Notes', 0)
    myInfo = ""
    mytX_tM = " "
    if client is not None:
        myInfo = client.allInfo()
        mytX_tM = client.Cal_interval()  # 接收返回的时间间隔
        for item in mytX_tM:
            text.addText().add_paragraph(item, style='Normal')
    text.saveText()
    return "Conference papers are saved"


#
# @app.get("/addinfo")  # 将转写的内容写入到word文档
# async def add_info(msg: str):
#     curTime = time.time()
#     global text
#     global client
#     text = Text()
#     myInfo = ""
#     #myInfo2 = msg
#     # if client is not None:
#     #     myInfo = client.allInfo()
#     #
#     #     for item in myInfo:
#     #         text.addText().add_paragraph(item)
#     text.addText().add_paragraph(msg)
#     #text.addText().add_paragraph(curTime)
#     text.saveText()
#     return "Conference papers are saved"

#
# @app.get("/savetxt")
# async def save_text(msg: str):
#     curTime = time.time()
#
#     myInfo = msg
#     print(msg)
#     # if client is not None:
#     #     myInfo = client.allInfo()
#     #
#     #     for item in range(myInfo):
#     #         text.addText().add_paragraph(item)
#     # text.saveText()
#     # return "Conference papers are saved"


@app.get("/download/{file_name}")  # fastAPI文件下载
def dnld_file(file_name: str):
    print(file_name)
    basedir = os.path.abspath(os.path.dirname(__file__))
    path = basedir + '\\' + "files"
    file_path = path + '\\' + str(datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")) + '.docx'
    print("The requested file path:::" + file_path)
    return FileResponse(file_path)


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host="127.0.0.1", port=8000)
